<template>
  <div class="page-container">
    <!-- 顶部标题栏 -->
    <div class="header">
      <h1 class="title">我的订单</h1>
    </div>

    <!-- 订单状态标签 -->
    <div class="order-tabs">
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'all' }"
        @click="activeTab = 'all'"
      >
        全部
      </div>
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'pending' }"
        @click="activeTab = 'pending'"
      >
        待付款
      </div>
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'processing' }"
        @click="activeTab = 'processing'"
      >
        待发货
      </div>
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'shipped' }"
        @click="activeTab = 'shipped'"
      >
        待收货
      </div>
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'completed' }"
        @click="activeTab = 'completed'"
      >
        已完成
      </div>
    </div>

    <!-- 订单列表 -->
    <div class="order-list" v-if="filteredOrders.length">
      <div 
        class="order-item" 
        v-for="order in filteredOrders" 
        :key="order.id"
        @click="goToOrderDetail(order.id)"
      >
        <div class="order-header">
          <div class="order-info">
            <span class="order-number">订单号：{{ order.orderNumber }}</span>
            <span class="order-time">{{ order.createTime }}</span>
          </div>
          <div class="order-status" :class="order.status">
            {{ getStatusText(order.status) }}
          </div>
        </div>

        <div class="order-products">
          <div 
            class="product-item" 
            v-for="product in order.products" 
            :key="product.id"
          >
            <div class="product-image">
              <img :src="product.image" :alt="product.name" />
            </div>
            <div class="product-info">
              <h4 class="product-name">{{ product.name }}</h4>
              <div class="product-spec" v-if="product.spec">{{ product.spec }}</div>
              <div class="product-price">
                <span class="price">¥{{ product.price }}</span>
                <span class="quantity">x{{ product.quantity }}</span>
              </div>
            </div>
          </div>
        </div>

        <div class="order-footer">
          <div class="order-total">
            共{{ getTotalQuantity(order.products) }}件商品
            合计：<span class="total-price">¥{{ getTotalPrice(order.products) }}</span>
          </div>
          
          <div class="order-actions">
            <nut-button 
              v-if="order.status === 'pending'" 
              type="primary" 
              size="small"
              @click.stop="payOrder(order.id)"
            >
              立即付款
            </nut-button>
            
            <nut-button 
              v-if="order.status === 'shipped'" 
              type="primary" 
              size="small"
              @click.stop="confirmReceive(order.id)"
            >
              确认收货
            </nut-button>
            
            <nut-button 
              v-if="order.status === 'completed'" 
              type="primary" 
              size="small"
              @click.stop="reviewOrder(order.id)"
            >
              评价订单
            </nut-button>
            
            <nut-button 
              v-if="order.status === 'pending'" 
              type="default" 
              size="small"
              @click.stop="cancelOrder(order.id)"
            >
              取消订单
            </nut-button>
            
            <nut-button 
              v-if="order.status === 'completed'" 
              type="default" 
              size="small"
              @click.stop="rebuyOrder(order.id)"
            >
              再次购买
            </nut-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div class="empty-state" v-else>
      <img src="https://picsum.photos/120/120?random=1" alt="暂无订单" />
      <h3>暂无订单</h3>
      <p>快去挑选心仪的商品吧</p>
      <nut-button type="primary" @click="goToHome">去逛逛</nut-button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

// 当前选中的标签
const activeTab = ref('all')

// 订单数据
const orders = ref([
  {
    id: 1,
    orderNumber: 'EN202401150001',
    createTime: '2024-01-15 14:30:25',
    status: 'pending',
    products: [
      {
        id: 1,
        name: 'iPhone 15 Pro Max 256GB',
        spec: '深空黑色 256GB',
        price: 9999,
        quantity: 1,
        image: 'https://picsum.photos/80/80?random=1'
      }
    ]
  },
  {
    id: 2,
    orderNumber: 'EN202401140002',
    createTime: '2024-01-14 10:15:30',
    status: 'processing',
    products: [
      {
        id: 2,
        name: '华为 Mate 60 Pro 512GB',
        spec: '雅川青 512GB',
        price: 6999,
        quantity: 1,
        image: 'https://picsum.photos/80/80?random=1'
      },
      {
        id: 3,
        name: 'AirPods Pro 3',
        spec: '白色',
        price: 1899,
        quantity: 1,
        image: 'https://picsum.photos/80/80?random=1'
      }
    ]
  },
  {
    id: 3,
    orderNumber: 'EN202401130003',
    createTime: '2024-01-13 16:45:12',
    status: 'shipped',
    products: [
      {
        id: 4,
        name: 'MacBook Pro M3 14英寸',
        spec: '深空黑色 512GB',
        price: 14999,
        quantity: 1,
        image: 'https://picsum.photos/80/80?random=1'
      }
    ]
  },
  {
    id: 4,
    orderNumber: 'EN202401120004',
    createTime: '2024-01-12 09:20:45',
    status: 'completed',
    products: [
      {
        id: 5,
        name: '小米 14 Ultra 16GB+1TB',
        spec: '黑色 16GB+1TB',
        price: 5999,
        quantity: 1,
        image: 'https://picsum.photos/80/80?random=1'
      }
    ]
  }
])

// 过滤后的订单
const filteredOrders = computed(() => {
  if (activeTab.value === 'all') {
    return orders.value
  }
  return orders.value.filter(order => order.status === activeTab.value)
})

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    pending: '待付款',
    processing: '待发货',
    shipped: '待收货',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statusMap[status] || '未知状态'
}

// 获取商品总数量
const getTotalQuantity = (products) => {
  return products.reduce((total, product) => total + product.quantity, 0)
}

// 获取订单总价格
const getTotalPrice = (products) => {
  return products.reduce((total, product) => total + (product.price * product.quantity), 0)
}

// 跳转到订单详情
const goToOrderDetail = (orderId) => {
  router.push(`/order-detail/${orderId}`)
}

// 支付订单
const payOrder = (orderId) => {
  showToast('跳转到支付页面')
}

// 确认收货
const confirmReceive = (orderId) => {
  showToast('确认收货成功')
  // 更新订单状态
  const order = orders.value.find(o => o.id === orderId)
  if (order) {
    order.status = 'completed'
  }
}

// 评价订单
const reviewOrder = (orderId) => {
  showToast('跳转到评价页面')
}

// 取消订单
const cancelOrder = (orderId) => {
  showToast('订单已取消')
  // 更新订单状态
  const order = orders.value.find(o => o.id === orderId)
  if (order) {
    order.status = 'cancelled'
  }
}

// 再次购买
const rebuyOrder = (orderId) => {
  showToast('已添加到购物车')
}

// 跳转到首页
const goToHome = () => {
  router.push('/')
}

// 显示提示
const showToast = (message) => {
  console.log(message)
}

onMounted(() => {
  // 从路由参数获取状态
  const status = route.query.status
  if (status) {
    activeTab.value = status
  }
})
</script>

<style scoped>
.header {
  background: white;
  padding: 16px;
  text-align: center;
  border-bottom: 1px solid #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.order-tabs {
  background: white;
  display: flex;
  border-bottom: 1px solid #f0f0f0;
  position: sticky;
  top: 66px;
  z-index: 99;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 16px 8px;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.3s;
}

.tab-item.active {
  color: #ff4757;
  border-bottom-color: #ff4757;
}

.order-list {
  flex: 1;
}

.order-item {
  background: white;
  margin: 12px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  background: #f8f8f8;
}

.order-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.order-number {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.order-time {
  font-size: 12px;
  color: #999;
}

.order-status {
  font-size: 14px;
  font-weight: 500;
  padding: 4px 8px;
  border-radius: 4px;
}

.order-status.pending {
  color: #ff9500;
  background: rgba(255, 149, 0, 0.1);
}

.order-status.processing {
  color: #007aff;
  background: rgba(0, 122, 255, 0.1);
}

.order-status.shipped {
  color: #34c759;
  background: rgba(52, 199, 89, 0.1);
}

.order-status.completed {
  color: #8e8e93;
  background: rgba(142, 142, 147, 0.1);
}

.order-status.cancelled {
  color: #ff3b30;
  background: rgba(255, 59, 48, 0.1);
}

.order-products {
  padding: 16px;
}

.product-item {
  display: flex;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.product-item:last-child {
  border-bottom: none;
}

.product-image {
  width: 60px;
  height: 60px;
  flex-shrink: 0;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

.product-info {
  flex: 1;
}

.product-name {
  font-size: 14px;
  color: #333;
  margin: 0 0 4px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-spec {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}

.product-price {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  font-size: 14px;
  color: #ff4757;
  font-weight: 500;
}

.quantity {
  font-size: 12px;
  color: #999;
}

.order-footer {
  padding: 16px;
  border-top: 1px solid #f0f0f0;
  background: #f8f8f8;
}

.order-total {
  text-align: right;
  font-size: 14px;
  color: #333;
  margin-bottom: 12px;
}

.total-price {
  font-size: 16px;
  color: #ff4757;
  font-weight: 600;
}

.order-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
  color: #999;
}

.empty-state img {
  width: 120px;
  height: 120px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state h3 {
  font-size: 18px;
  color: #666;
  margin: 0 0 8px 0;
}

.empty-state p {
  font-size: 14px;
  color: #999;
  margin: 0 0 24px 0;
}
</style>
